<?php

namespace app\common\model\goods;

use think\Model;
use traits\model\SoftDelete;

class Goods extends Model
{

    use SoftDelete;



    // 表名
    protected $name = 'goods';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'status_text',
        'type_text',
        'hot_text',
        'image_str',
        'images_array',
        'total_stock',
    ];


    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
        self::beforeInsert(function ($row) {
            $changed = $row->getChangedData();
            //如果有添加规格
            if (isset($changed['specjson'])) {
                $spec_array = json_decode($changed['specjson'], true);
                if (in_array($changed['type'], [1, 2, 4])) {
                    $row->min_original_price = min(array_column($spec_array, 'original_price'));
                }
                $row->min_fee = min(array_column($spec_array, 'price'));
            }
        });
        self::beforeUpdate(function ($row) {
            $type = $row->origin['type'];

            $changed = $row->getChangedData();

            //如果有修改规格
            if (isset($changed['specjson'])) {
                $spec_array = json_decode($changed['specjson'], true);
                if (in_array($type, [1, 2, 4])) {
                    $row->min_original_price = min(array_column($spec_array, 'original_price'));
                }
                $row->min_fee = min(array_column($spec_array, 'price'));
            }
        });
    }
    public function getTotalStockAttr($v, $d)
    {
        return Spec::where('goods_id', $d['id'])->sum('stock');
    }
    public function getImageStrAttr($v, $d)
    {
        return $d['image'] ? cdnurl($d['image'], true) : '';
    }
    public function getImagesArrayAttr($v, $d)
    {
        if ($d['images']) {
            $array = explode(',', $d['images']);
            foreach ($array as &$value) {
                $value = $value ? cdnurl($value, true) : '';
            }
            return $array;
        }
        return [];
    }
    public function suk()
    {
        return $this->hasMany(Spec::class, 'goods_id', 'id');
    }
    public function getStatusList()
    {
        return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
    }

    public function getTypeList()
    {
        return ['1' => __('Type 1'), '2' => __('Type 2'), '3' => __('Type 3')];
    }

    public function getHotList()
    {
        return ['0' => __('Hot 0'), '1' => __('Hot 1')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getHotTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['hot']) ? $data['hot'] : '');
        $list = $this->getHotList();
        return isset($list[$value]) ? $list[$value] : '';
    }
}
